﻿using LibDll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDDY_VehicleRepo
{
    public partial class FrmFDJInfo : Form
    {
        public FrmFDJInfo(string strType, string strID)
        {
            InitializeComponent();
            if (strType == "View")
            {
                this.Text = "发动机详情-" + "查看";
                btnSaveFDJ.Enabled = false;
            }
            else if (strType == "Update")
            {
                this.Text = "发动机详情-" + "修改";
                btnSaveFDJ.Enabled = true;
            }
            if (strType == "Add")
            {
                this.Text = "发动机详情-" + "添加";
            }

            this.BindFDJInfo(strID);
        }

        private void BindFDJInfo(string strID)
        {
            try
            {
                var vConfig = Common.GetAppConfig();

                DataTable dt = new DataTable();
                string strSql = "select * from FdjRepository where ID='" + strID + "'";
                DbHelper.GetTable(vConfig[0], strSql, ref dt);

                if (dt.Rows.Count > 0)
                {
                    txtFdjbs.Text = dt.Rows[0]["fdjbs"].ToString().Trim();
                    txtFdjsb.Text = dt.Rows[0]["fdjsb"].ToString().Trim();
                    txtFdjscdz.Text = dt.Rows[0]["fdjsccdz"].ToString().Trim();
                    string strRlzl = dt.Rows[0]["rlzl"].ToString().Trim();
                    if (strRlzl.Length > 0)
                    {
                        for (int i = 0; i < strRlzl.Length; i++)
                        {
                            string strR = strRlzl.Substring(i, 1);

                            for (int j = 0; j < chkListRlzl.Items.Count; j++)
                            {
                                if (chkListRlzl.Items[j].ToString().Contains(strR))
                                {
                                    chkListRlzl.SetItemChecked(j, true);
                                }
                            }
                        }
                    }

                    txtEdzs.Text = dt.Rows[0]["edzs"].ToString();
                    txtFdjpl.Text = dt.Rows[0]["fdjpl"].ToString();
                    txtEdgl.Text = dt.Rows[0]["edgl"].ToString();
                    txtEdzs.Text = dt.Rows[0]["edzs"].ToString();
                    txtEdzs.Text = dt.Rows[0]["edzs"].ToString();
                    string strChzhqqk = dt.Rows[0]["chzhqk"].ToString();
                    for (int j = 0; j < cmbChzhqqk.Items.Count; j++)
                    {
                        if (cmbChzhqqk.Items[j].ToString().Contains(strChzhqqk))
                        {
                            cmbChzhqqk.SelectedIndex = j;
                            break;
                        }
                    }
                    txtFdjqgs.Text = dt.Rows[0]["fdjqgs"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSaveFDJ_Click(object sender, EventArgs e)
        {
            try
            {
                string strFdjbs = txtFdjbs.Text.Trim();
                if (string.IsNullOrEmpty(strFdjbs))
                {
                    MessageBox.Show("发动机标识不能为空!");
                    return;
                }

                string strFdjsb = txtFdjsb.Text.Trim();
                string strFdjsccdz = txtFdjscdz.Text.Trim();
                string strRlzl = string.Empty;

                for (int j = 0; j < chkListRlzl.CheckedItems.Count; j++)
                {
                    strRlzl += chkListRlzl.CheckedItems[j].ToString().Substring(0, 1);
                }

                string strEdzs = txtEdzs.Text.Trim();

                if (!string.IsNullOrEmpty(strEdzs))
                {
                    int iEdzs;
                    if (int.TryParse(strEdzs, out iEdzs) == false)
                    {
                        MessageBox.Show("额定转速不是整数!");
                        return;
                    }
                }

                string strFdjpl = txtFdjpl.Text.Trim();
                string strEdgl = txtEdgl.Text.Trim();
                string strChzhqqk = cmbChzhqqk.Text.Substring(0, 1);
                string strFdjqgs = txtFdjqgs.Text.Trim();

                FdjRepository fdjRepository = new FdjRepository();
                fdjRepository._fdjbs = strFdjbs;
                fdjRepository._chzhqk = strChzhqqk;
                fdjRepository._edgl = strEdgl;
                fdjRepository._edzs = strEdzs;
                fdjRepository._fdjpl = strFdjpl;
                fdjRepository._fdjqgs = strFdjqgs;
                fdjRepository._fdjsb = strFdjsb;
                fdjRepository._fdjsccdz = strFdjsccdz;
                fdjRepository._rlzl = strRlzl;

                var vRet = SaveFdjRepository(fdjRepository);
                if (vRet.IsSuccess)
                {
                    MessageBox.Show("保存成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("保存失败," + vRet.Message);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败," + ex.Message);
            }
        }

        /// <summary>
        /// 保存发动机信息
        /// </summary>
        /// <param name="fdjRepository"></param>
        /// <returns></returns>
        private ReturnInfo SaveFdjRepository(FdjRepository fdjRepository)
        {
            ReturnInfo ret = new ReturnInfo();
            try
            {
                var vConfig = Common.GetAppConfig();
                string strSql = "select count(*) from FdjRepository where fdjbs='" + fdjRepository._fdjbs + "'";

                string strValue = DbHelper.GetValue(vConfig[0], strSql);

                if (Convert.ToInt32(strValue) == 0)
                {
                    strSql = " INSERT INTO[dbo].[FdjRepository] ";
                    strSql += "(";
                    strSql += "[fdjbs]";
                    strSql += ",[fdjsb]";
                    strSql += ",[fdjsccdz]";
                    strSql += ",[rlzl]";
                    strSql += ",[edzs]";
                    strSql += ",[fdjpl]";
                    strSql += ",[edgl]";
                    strSql += ",[fdjqgs]";
                    strSql += ",[chzhqk])";
                    strSql += "VALUES";
                    strSql += "(";
                    strSql += "'" + fdjRepository._fdjbs + "',";
                    strSql += "'" + fdjRepository._fdjsb + "',";
                    strSql += "'" + fdjRepository._fdjsccdz + "',";
                    strSql += "'" + fdjRepository._rlzl + "',";
                    strSql += "'" + fdjRepository._edzs + "',";
                    strSql += "'" + fdjRepository._fdjpl + "',";
                    strSql += "'" + fdjRepository._edgl + "',";
                    strSql += "'" + fdjRepository._fdjqgs + "')";

                    var vRet = DbHelper.ExecuteInsertSql(vConfig[0], strSql);
                    if (vRet.IsSuccess == false)
                    {
                        ret.IsSuccess = false;
                        ret.Message = strSql;
                    }
                    else
                    {
                        ret.IsSuccess = true;
                    }
                }
                else
                {
                    strSql = "UPDATE [FdjRepository]";
                    strSql += " SET ";
                    //strSql += " [fdjbs] ='" + fdjRepository._fdjbs + "',";
                    strSql += " [fdjsb] ='" + fdjRepository._fdjsb + "',";
                    strSql += " [fdjsccdz] ='" + fdjRepository._fdjsccdz + "',";
                    strSql += " [rlzl] ='" + fdjRepository._rlzl + "',";
                    strSql += " [edzs] ='" + fdjRepository._edzs + "',";
                    strSql += " [fdjpl] ='" + fdjRepository._fdjpl + "',";
                    strSql += " [edgl] ='" + fdjRepository._edgl + "',";
                    strSql += " [fdjqgs] ='" + fdjRepository._fdjqgs + "',";
                    strSql += " [chzhqk] ='" + fdjRepository._chzhqk + "' ";
                    strSql += " where fdjbs='" + fdjRepository._fdjbs + "'";

                    var vRet = DbHelper.ExecuteInsertSql(vConfig[0], strSql);
                    if (vRet.IsSuccess == false)
                    {
                        ret.IsSuccess = false;
                        ret.Message = strSql;
                    }
                    else
                    {
                        ret.IsSuccess = true;
                    }
                }
            }
            catch(Exception ex)
            {
                ret.IsSuccess = false;
                ret.Message = ex.Message;
            }

            return ret;
        }

        private void btnCloseFDJ_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
